Page({
  data: {
    num: '', // 阿拉伯数字
    chineseNum: '' // 中文数字
  },
  // 将阿拉伯数字转换为中文数字
  convertNumberToChinese: function(num) {
    const numUnits = [
      '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'
    ];
    const bigUnits = [
      '', '拾', '佰', '仟'
    ];

    if (isNaN(num) || num < 0) {
      return '无效输入';
    }

    let strOutput = '';
    let numStr = num.toString();
    let len = numStr.length;

    for (let i = 0; i < len; i++) {
      let currentNum = parseInt(numStr[i], 10);
      if (currentNum !== 0) {
        strOutput += numUnits[currentNum];
        if (i !== len - 1) {
          strOutput += bigUnits[len - i - 1 % 4];
        }
      }
    }

    return strOutput === '' ? '零' : strOutput;
  },
  // 绑定输入框的输入事件，实现转换
  bindNumberInput: function(e) {
    const num = e.detail.value;
    if (e.detail.cursor > 0 || num === '') {
      this.setData({
        num: num,
        chineseNum: this.convertNumberToChinese(num)
      });
    }
  }
});